<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import {confirmModal,getEditor,errorModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				userCacheKey:"hr-u-tempInstAdjustMulti-planPosAndEmpInfo-user-cache",
				posId:'',
				instId:"",
				formCode:"",
				posCacheKey:"hr-u-tempInstAdjustMulti-planPosAndEmpInfo-pos-cache",
				posList:[],
				posListCache:'hr-u-tempInstAdjustMulti-planPosAndEmpInfo-posList-cache',
				uId:"",
				edit:"",
				adjustedSalaryPostCacheKey:"hr-u-tempInstAdjustMulti-planPosAndEmpInfo-adjustedSalaryPost-cache"
			})
		},
		methods:{
			onSelectPlanWorkWay(item){
				let _that = this
				if(_that.datas.length){
					_that.datas.map(e=>{
						if(e.userId === _that.$refs.autoform.formdata.userId && e.planWorkWayCode === "20388"){
							errorModal(_that,"提示信息","选择"+item.dicname+"将清空"+e.planWorkWayName+"的任职方式,是否继续?")
						}else{
							confirmModal(_that,"提示信息","选择"+item.dicname+"将会调整原担任岗位的任职方式,是否继续?",()=>{},()=>{
								_that.$refs.autoform.clear("planPosAndEmpInfo","planWorkWayCode")
							})
						}
					})
				}else{
					confirmModal(_that,"提示信息","选择"+item.dicname+"将会调整原担任岗位的任职方式,是否继续?",()=>{},()=>{
						_that.$refs.autoform.clear("planPosAndEmpInfo","planWorkWayCode")
					})
				}
				
			},
			initDatas(){
				let _that = this
				_that.om.request(
					"tempInstAdjustMulti/getDatas",
					{
						posId: _that.$refs.autoform.formdata.posId
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.formdata.salaryWay = res.value.salaryWay
					_that.$refs.autoform.formdata.posCareercat = res.value.posCareercat
					_that.$refs.autoform.formdata.gradeSalaryRange = res.value.gradeSalaryRange
				})
			},
			getWorkType(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getWorkType",
					{
						userId:id
					}
				).then(res=>{
					_that.initPlanWorkWay(res.value.posId)
				})
			},
			initPlanWorkWay(id){
				let _that = this
				_that.om.request(
					"tempInstAdjustMulti/planWorkWayCode",
					{
						posId: _that.$refs.autoform.formdata.posId,
						userId: _that.$refs.autoform.formdata.userId,
						appointPosId:id,
						reasonCode:_that.$refs.autoform.formdata.reasonCode 
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor('planPosAndEmpInfo',"planWorkWayCode","type","selectbox")
					_that.$refs.autoform.changeEditor('planPosAndEmpInfo',"planWorkWayCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex('planPosAndEmpInfo',"planWorkWayCode")
					_that.$refs.autoform.changeEditor('planPosAndEmpInfo',"planWorkWayCode","onSelect","onSelectPlanWorkWay") 
				})
			},
			verifyReso2(){
				let _that = this
				_that.om.request(
					"hrPosUtil/getTipAfterUser",
					{
						instId: _that.instId,
						formCode: _that.formCode,
						userId:_that.formdata.userId,
						zposId: _that.posId
					}
				).then(res=>{
						_that.posList = res.value.attributes.pos
						_that.$refs.autoform.changeEditor("planPosAndEmpInfo","posId","type","linkbox")
						_that.$refs.autoform.changeEditor("planPosAndEmpInfo","posId",{
							link:"../../public/pageSearch/pageSearch",
							"linkParam":{
								other:JSON.stringify({}),
								namefield:"posname",
								initload:true,
								defaultData:JSON.stringify(_that.posList),
								cachename:_that.posListCache
							}
						})
						_that.getSalaryPostList()
						
				})
			},
			verifyReson1(){
				let _that = this
				_that.om.request(
					"hrPosUtil/verifyReason",{
						formCode: _that.formCode,
						userId: _that.$refs.autoform.formdata.userId
					},
					"post"
				).then(res=>{
					if(res.value){
						errorModal(_that,"提示信息",ignoreHtml(res.value),()=>{
							_that.$refs.autoform.clear("planPosAndEmpInfo","reasonCode")
						})
					}
				})
			},
			onSelectReson(item){
				let _that = this
				switch(item.code){
					case '23753':
						_that.verifyReson1()
					break;
					case '23758':
						_that.verifyReso2()
					break;
					default:
					break;
				}
			},
			getResonList(){
				let _that = this
				_that.om.request(
					"tempInstAdjustMulti/getReasonCodeList",
					{
						instId:_that.instId
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("planPosAndEmpInfo","reasonCode","type","selectbox")
					_that.$refs.autoform.changeEditor("planPosAndEmpInfo","reasonCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex("planPosAndEmpInfo","reasonCode")
				})
			},
			verifyAdjust(userId){
				let _that = this
				_that.om.request(
					"common/verifyAdjust",
					{
						userId:userId
					},
					"post"
				).then(res=>{
					if(res.value){
						errorModal(_that,"提示信息",ignoreHtml(res.value),()=>{
							_that.$refs.autoform.clear("planPosAndEmpInfo","userId")
							_that.getSalaryPostList()
						})
					}else{
						_that.afterUser(userId)
					}
					
				})
			},
			afterUser(userId){
				let _that = this
				_that.om.request(
					"hrPosUtil/getTipAfterUser",
					{
						instId: _that.instId,
						formCode: _that.formCode,
						userId:userId,
						zposId: _that.posId
					}
				).then(res=>{
						_that.getResonList()
						_that.posList = res.value.attributes.pos
						if(_that.edit =="true"){
							_that.verifyReso2()
						}
						
				})
			},
			getSalaryPostList() {
				let _that = this
				let postIdList = [];
				let resignFlag = false;
				if(_that.$refs.autoform.formdata.userId && _that.$refs.autoform.formdata.posId){
					for(let i=0; i<_that.datas.length; i++){
						if(_that.$refs.autoform.formdata.userId == _that.datas[i].userId){
							if(i != _that.index){
								let rowPostId = _that.datas[i].posId;
								if (rowPostId) {
								    postIdList.push(rowPostId)
								    if(_that.datas[i].resignFlag){
								        resignFlag = _that.datas[i].resignFlag;
								    }
								}
							}else{
								postIdList.push(_that.$refs.autoform.formdata.posId)
								let rowResignFlag = _that.$refs.autoform.formdata.resignFlag
								if(rowResignFlag){
								    resignFlag =  rowResignFlag;
								}
							}
						}
					}
				}
				
				_that.$refs.autoform.changeEditor("planPosAndEmpInfo","adjustedSalaryPostId", {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							userId: _that.$refs.autoform.formdata.userId,
							postIdStr: postIdList.toString(),
							resignFlag: resignFlag
						}),
						namefield:"posName",
						notefield:'instName',
						initload:true,
						searchfield:'q',
						url:'tempInstAdjustMulti/getSalaryPostList',
						cachename:_that.adjustedSalaryPostCacheKey,
						prompt: "请输入关键字查询"
					}
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userName = d.username
							formdata.userId = d.id
							formdata.oldPostId =  d.salaryPosId
							formdata.oldPostName = d.salaryPosName
							formdata.oldInstId =  d.salaryPosInstId
							formdata.oldInstName = d.salaryPosInstName
							_that.uId = d.id
							_that.verifyAdjust(d.id)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.adjustedSalaryPostCacheKey,
					success:(res)=>{
						console.log(res)
						// if(res.data){
						// 	let d = res.data
						// 	let formdata = _that.$refs.autoform.formdata
						// 	formdata.userName = d.username
						// 	formdata.userId = d.id
							
						// 	uni.removeStorage({
						// 		key:_that.adjustedSalaryPostCacheKey
						// 	})
						// }
					}
				})
				uni.getStorage({
					key:_that.posListCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							let isChoosed = false
							for(let item of _that.datas){
								if(res.posId === d.posid && res.userId === formdata.userId){
									errorModal(_that,"提示信息",formdata.userName+"已选择过"+d.posname+"，请重新选择！",()=>{
										_that.isChoosed = true
									})
								}else{
									_that.isChoosed = false
								}
							}
						
							if(!isChoosed){
								formdata.posName = d.posname
								formdata.posId = d.posid
								_that.getWorkType(_that.uId)
								_that.initDatas()
								_that.getSalaryPostList()
							}else{
								_that.$refs.autoform.formdata.salaryWay = ""
								_that.$refs.autoform.formdata.posCareercat = ""
								_that.$refs.autoform.formdata.gradeSalaryRange = ""
							}
						
							uni.removeStorage({
								key:_that.posListCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.posId = e.posId
								_that.instId= e.instId
								_that.formCode= e.formCode
								_that.edit = e.edit
								changeEditor("planPosAndEmpInfo","userId",{
									link:"../../public/pageSearch/pageSearch",
									"linkParam":{
										other:JSON.stringify({
										}),
										namefield:"username",
										extrafield:'userno',
										notefield:'posInstName',
										initload:true,
										searchfield:'q',
										url:'temployee/getStaffCombogrid',
										cachename:_that.userCacheKey
									}
								},null,_that.autoformdata)
								
								if(_that.formdata.userId){
									changeEditor("planPosAndEmpInfo","reasonCode","data",[],_that.autoformdata)
									changeEditor("planPosAndEmpInfo","reasonCode","type","selectbox",_that.autoformdata)
									_that.om.request(
										"tempInstAdjustMulti/getReasonCodeList",
										{
											instId:_that.instId
										},
										"post"
									).then(res=>{
										_that.$refs.autoform.changeEditor("planPosAndEmpInfo","reasonCode","data",configSelectData(res,"code","dicname"))
										_that.$nextTick(function(){
											_that.$refs.autoform.initSelectIndex("planPosAndEmpInfo","reasonCode")
										})
									})
								}
								if(_that.formdata.reasonCode){
									changeEditor("planPosAndEmpInfo", "posId", "type",
										"linkbox", _that.autoformdata)
									_that.om.request(
										"hrPosUtil/getTipAfterUser", {
											instId: _that.instId,
											formCode: _that.formCode,
											userId: _that.formdata.userId,
											// zposId: _that.posId
										}
									).then(res => {
										_that.$refs.autoform.changeEditor("planPosAndEmpInfo", "posId", {
											link: "../../public/pageSearch/pageSearch",
											"linkParam": {
												other: JSON.stringify({}),
												namefield: "posname",
												initload: true,
												defaultData: JSON.stringify(res.value
													.attributes.pos),
												cachename: _that.posListCache
											}
										}, _that.autoformdata)
									})
								}
								if(_that.formdata.userId && _that.formdata.reasonCode && _that.formdata.posId){
									changeEditor('planPosAndEmpInfo',"planWorkWayCode","type","selectbox", _that.autoformdata)
									changeEditor('planPosAndEmpInfo',"planWorkWayCode","data",[], _that.autoformdata)
									_that.om.request(
										"orgInstPosUserOfficial/getWorkType",
										{
											userId:_that.formdata.userId
										}
									).then(res=>{
										_that.om.request(
											"tempInstAdjustMulti/planWorkWayCode",
											{
												posId: _that.formdata.posId,
												userId: _that.formdata.userId,
												appointPosId:res.value.posId,
												reasonCode:_that.$refs.autoform.formdata.reasonCode 
											},
											"post"
										).then(res1=>{
											_that.$refs.autoform.changeEditor('planPosAndEmpInfo',"planWorkWayCode","data",configSelectData(res1,"code","dicname"))
											_that.$refs.autoform.changeEditor('planPosAndEmpInfo',"planWorkWayCode","onSelect","onSelectPlanWorkWay") 
											_that.$refs.autoform.initSelectIndex('planPosAndEmpInfo',"planWorkWayCode")
										})
									})
								}
								_that.$nextTick(function(){
									_that.getSalaryPostList()
								})
								
							}
						}
					})
				}			
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                  
